﻿#include "Gate.h"
#include "../../../Shared/ServerPackets.h"
#include "../../../Shared/Data/Stat.h"

using namespace Server::MirDatabase;
using namespace Server::MirEnvir;
namespace S = ServerPackets;

namespace Server::MirObjects::Monsters
{

    Gate::Gate(MonsterInfo *info) : CastleGate(info)
    {
        switch (info->Effect)
        {
            case 1: //Sabuk Door
                BlockArray = std::vector<Point*>
                {
                    new Point(0, -1),
                    new Point(0, -2),
                    new Point(1, -1),
                    new Point(1, -2),
                    new Point(-1, 0),
                    new Point(-2, 0),
                    new Point(-1, -1),
                    new Point(-1, 1)
                };
                break;
            case 2: //West Pointing Castle Gi
                BlockArray = std::vector<Point*>
                {
                    new Point(0, -1),
                    new Point(-1, -1),
                    new Point(-1, 0),
                    new Point(0, 1),
                    new Point(1, 1),
                    new Point(1, 0),
                    new Point(1, -1),
                    new Point(0, -2)
                };
                break;
            case 3: // South Pointing Castle Gi
            {
                    BlockArray = std::vector<Point*>
                    {
                        new Point(1, -1),
                        new Point(0, -1),
                        new Point(0, -2),
                        new Point(-1, -1),
                        new Point(-2, -1),
                        new Point(-1, 0),
                        new Point(-1, 1),
                        new Point(-2, 0)
                    };
            }
                break;
            case 4: // East Pointing Castle Gi
            {
                    BlockArray = std::vector<Point*>
                    {
                        new Point(-2, 0),
                        new Point(-1, 1),
                        new Point(0, 2),
                        new Point(0, 1),
                        new Point(-1, 0),
                        new Point(-1, -1),
                        new Point(1, 1)
                    };

            }
                break;
        }

        setDirection(MirDirection::Up);
    }

    void Gate::Despawn()
    {
        CastleGate::Despawn();
    }

    int Gate::Attacked(PlayerObject *attacker, int damage, DefenceType type, bool damageWeapon)
    {
        CheckDirection();

        if (!Conquest->getWarIsOn() || attacker->MyGuild != nullptr && Conquest->Owner == attacker->MyGuild->Guildindex)
        {
            damage = 0;
        }

        return CastleGate::Attacked(attacker, damage, type, damageWeapon);
    }

    int Gate::Attacked(MonsterObject *attacker, int damage, DefenceType type)
    {
        CheckDirection();

        if (!Conquest->getWarIsOn())
        {
            damage = 0;
        }

        return CastleGate::Attacked(attacker, damage, type);
    }

    void Gate::OpenDoor()
    {
        if (!Closed)
        {
            return;
        }

        setDirection(static_cast<MirDirection>(6));
        Closed = false;

        S::ObjectAttack *tempVar = new S::ObjectAttack();
        tempVar->ObjectID = ObjectID;
        tempVar->Direction = getDirection();
        tempVar->Location = getCurrentLocation();
        tempVar->Type = 0;
        Broadcast(tempVar);

        ActiveDoorWall(false);

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
    }

    void Gate::CheckDirection()
    {
        MirDirection newDirection = static_cast<MirDirection>(3 - GetDamageLevel());

        if (newDirection != getDirection())
        {
            setDirection(newDirection);
            S::ObjectTurn *tempVar = new S::ObjectTurn();
            tempVar->ObjectID = ObjectID;
            tempVar->Direction = getDirection();
            tempVar->Location = getCurrentLocation();
            Broadcast(tempVar);

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
        }
    }

    void Gate::CloseDoor()
    {

        if (Closed)
        {
            return;
        }

        setDirection(static_cast<MirDirection>(3 - GetDamageLevel()));

        Closed = true;

        S::ObjectAttack *tempVar = new S::ObjectAttack();
        tempVar->ObjectID = ObjectID;
        tempVar->Direction = getDirection();
        tempVar->Location = getCurrentLocation();
        tempVar->Type = 1;
        Broadcast(tempVar);

        ActiveDoorWall(true);

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
    }

    void Gate::RepairGate()
    {
        if (HP == 0)
        {
            Revive(Stats[Stat::HP], false);
        }
        else
        {
            SetHP(Stats[Stat::HP]);
        }

        CheckDirection();
    }

    int Gate::GetDamageLevel()
    {
        int level = static_cast<int>(BankersRounding::round(static_cast<double>(3 * HP) / Stats[Stat::HP]));

        if (level < 1)
        {
            level = 1;
        }

        return level;
    }

    bool Gate::getCanRegen() const
    {
        return false;
    }
}
